/**
 * usage:
 * $("#grid").externalSearch("#form");
 * <sj:a button="true" onClickTopics="onSearch">search</sj:a>
 * subscribe the onSearch topic and trigger grid reload
 * 
 * @author Justin
 */
;(function($){
	$.fn.extend({
		externalSearch : function(formId){

			var grid = this;

			// the topic id : "onSearch_"+grid.id
			$.subscribe('onSearch_'+grid[0].id, function(event, data){
				triggerSearch();
			});
			
			var submitButton = formId + " input[type='submit']";
			var submitAnchor = formId + " #submit";
			var submitAnchor2 = formId + " .submit";
			$(submitButton + ", " + submitAnchor + ", " + submitAnchor2).click(function(){
				triggerSearch();
				return false;
			});

			var triggerSearch = function() {
				
				var fnBefore = grid.data("beforeSearch");
				if($.isFunction(fnBefore) && !fnBefore()) return;
				
				var gr = grid[0];
				
				
				
				// modify by Justin
				var sdata={};
				var params = $(formId).serializeArray();
				for(var index in params){
					var name = params[index].name;
					var value = params[index].value;
					if(value!=null && value!="")
					sdata[name] = value;
				}
				
				grid.publish("grid-filter", sdata);
			};// triggerSearch
			
		},// externalSearch
		
		/**
		 * enhance the grid!
		 */
		enhanceGrid: function(){
			var grid = this;
			
			/**
			 * can be use to simulate linkage
			 * @param data criteria object. eg: {id:1,name:"Bob"}
			 */
			function fnGridFilter(event, data){
				
				var gr = grid[0];
				
				for(var index in grid.data("preCriteria")){
					delete gr.p.postData[index];
				}
				
				$.extend(gr.p.postData, data);
				grid.jqGrid('setGridParam', {search:true,page:1});
				grid.trigger("reloadGrid");
				
				grid.data("preCriteria", data);
			}
			
			this.subscribe("grid-filter", fnGridFilter);
			this.bind("grid-filter", fnGridFilter);
			
			/**
			 * handle delete operation for the given grid
			 * publish events:
			 * "reload" when success
			 * "onDeleteFail" when error occurs
			 * @param data.url		url handles delete action
			 */
			function fnGridDelete(event, data){
				var reqParams;
				var selectedId	= grid.jqGrid('getGridParam','selrow');
				if(selectedId != null){
					reqParams = {id: selectedId};
				}
				var selectedIds = grid.jqGrid('getGridParam','selarrrow');
				if(selectedIds != null){
					reqParams = {ids: selectedIds};
				}
				if( reqParams != null ) {
					$.ajax({
						url: data.url,
						type: "POST",
						data: reqParams,
						success: function(){
							grid.trigger("reloadGrid");
							grid.publish("grid-delete-success");
						},// this callback run first
						error: function(data, textStatus){
							grid.publish("grid-delete-fail");
						}
					});
				}
			}
			this.subscribe("grid-delete", fnGridDelete);
			this.bind("grid-delete", function(event, data){
				fnGridDelete(event, data);
			});
			
			/**
			 * handle reload event, 
			 * TODO: reload current page?
			 */
			this.subscribe("grid-reload", function(event, data){
				grid.trigger("reloadGrid");
			});
		} // enhanceGrid
	});//extend
	
})(jQuery);//jq